package com.efast.dmom.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.QualityCheck;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.QualityCheckHandler;
import com.efast.dmom.mes.service.QualityCheckService;
@Service
public class QualityCheckServiceImpl implements QualityCheckService {

	@Autowired
	private QualityCheckHandler qualityCheckHandler;
	
	private List<QualityCheck> list = new ArrayList<QualityCheck>();
	
	//递归  通过id获取树下当前数据下所有的子集
	public List<QualityCheck> getAllchildByid(List<Long> id) {
		List<QualityCheck> listDataById = qualityCheckHandler.getListDataById(id);
		if (listDataById != null && listDataById.size()>0) {
//			list.addAll(listDataById);
			listDataById.forEach(item -> {
				list.add(item);
			});
			List<Long> idList = new ArrayList<Long>();
			listDataById.forEach(item -> {
				idList.add(item.getInnerData().getId());
			});
			getAllchildByid(idList);
		}
		return list;
	}
	
	@Override
	public ResultBean save(QualityCheck obj) {
		ResultBean resultBean = new ResultBean(true);
		List<QualityCheck> byparentidAndCode = qualityCheckHandler.getByparentidAndCode(obj);
		if (byparentidAndCode != null && byparentidAndCode.size() >0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("当前树下缺陷编码  "+obj.getInnerData().getCheckCode()+" 已存在！");
			return resultBean;
		}
		QualityCheck byId = qualityCheckHandler.getById(obj.getInnerData().getParentId());
		// 当查询到父id时，将level累加
		if (byId != null) {
			obj.getInnerData().setLevel(byId.getInnerData().getLevel()+1);
		}
		qualityCheckHandler.save(obj);
		return resultBean;
		
	}

	@Override
	public ResultBean delete(QualityCheck entity) {
		ResultBean resultBean = new ResultBean(true);
		return resultBean;
		
	}

	@Override
	public ResultBean update(QualityCheck obj) {
		ResultBean resultBean = new ResultBean(true);
		List<QualityCheck> byparentidAndCode = qualityCheckHandler.getByparentidAndCode(obj);
		if (byparentidAndCode.size() >0 && 
				!byparentidAndCode.get(0).getInnerData().getCheckCode().equals(obj.getInnerData().getCheckCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("当前检验编码已存在!");
			return resultBean;
		}
		qualityCheckHandler.update(obj);
		return resultBean;
		
	}

	@Override
	public QualityCheck getById(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public QualityCheck getByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ResultBean batchDeleteByIds(List<QualityCheck> list) {
		ResultBean resultBean = new ResultBean(true);
		List<Long> ids = new ArrayList<Long>();
		list.forEach(item -> {
			ids.add(item.getInnerData().getId());
		});
		List<QualityCheck> allchildByid = getAllchildByid(ids);
		if (allchildByid != null && allchildByid.size() >0) {
			list.addAll(allchildByid);
		}
		qualityCheckHandler.batchDeleteById(list);
		return resultBean;
	}
	@Override
	public ResultBean validateIfChildListById(List<QualityCheck> list) {
		ResultBean resultBean = new ResultBean(true);
		List<Long> ids = new ArrayList<Long>();
			list.forEach(item -> {
				ids.add(item.getInnerData().getId());
			});
		List<QualityCheck> listDataById = qualityCheckHandler.getListDataById(ids);
		if (listDataById.size() > 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("当前数据下存在子集  ");
		}
		return resultBean;
	}
	@Override
	public List<QualityCheck> getAllParentData() {
		List<QualityCheck> allParentData = qualityCheckHandler.getAllParentData();
		return allParentData;
	}
	@Override
	public ResultBean getChildByParentId(QualityCheck qualityCheck) {
		ResultBean resultBean = new ResultBean(true);
		List<QualityCheck> childByParentId = qualityCheckHandler.getChildByParentId(qualityCheck);
		resultBean.setObject(childByParentId);
		return resultBean;
	}
	
}
